package cn.fntop.context;

import cn.fntop.constant.TenantConstants;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.autoconfigure.DdlApplicationRunner;
import com.baomidou.mybatisplus.extension.ddl.IDdl;
import org.springframework.boot.ApplicationArguments;

import java.util.List;

/**
 * @author fn
 * @description 动态数据源SQL自动维护
 * @date 2024/1/29 10:13
 */
public class DsDdlApplicationRunner extends DdlApplicationRunner {
    public DsDdlApplicationRunner(List<IDdl> ddlList) {
        super(ddlList);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        TenantConstants.DS[] ds = TenantConstants.DS.values();
        for (int i = 0; i < ds.length; i++) {
            String dsKey = ds[i].getDsKey();
            //切换数据源执行升级SQL
            DynamicDataSourceContextHolder.push(dsKey);
            super.run(args);
        }
    }
}
